package com.yanqu.road.server.cmd.grandchild;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.grandchild.GrandChildProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.grandchild.GrandChildMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;

@Cmd(code = CrossProtocol.C_GRAND_CHILD_ENTER_IMPERIAL_EXAM, desc = "进入科举考场")
public class CrossEnterImperialExamCmd extends CrossCommand {

    private long userId;
    private int pageNum;
    private long reqServerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        userId = packet.getPlayerId();
        reqServerId = serverId;
        GrandChildProto.EnterImperialExamReqMsg reqMsg = GrandChildProto.EnterImperialExamReqMsg.parseFrom(packet.getBytes());
        pageNum = reqMsg.getPageNum();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ret = GrandChildMgr.enterExam(reqServerId, userId, pageNum);
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            GrandChildProto.EnterExamRespMsg.Builder tmp = GrandChildProto.EnterExamRespMsg.newBuilder();
            tmp.setRet(ret);
            YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_GRAND_CHILD_ENTER_IMPERIAL_EXAM, tmp);
            MessageHelper.sendPacket(serverId, userId, message);
        }
    }
}
